package com.mililoan.service.module.system.impl;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mililoan.common.bean.PageResultSet;
import com.mililoan.model.system.Actor;
import com.mililoan.persist.system.ActorDao;
import com.mililoan.persist.system.ActorRightDao;
import com.mililoan.service.module.system.ActorBo;

/**
 * 角色操作实现类
 * @author mwei
 *
 */

@Service
public class ActorBoImpl implements ActorBo {
	
    @Autowired
    private ActorDao actorDao;
    @Autowired
	private ActorRightDao actorRightDao;
	//默认角色名
	private String defaultActoName;

	/**
	 * 通过ID获得角色
	 * @param id 角色ID
	 * @return
	 */
	public Actor getActorById(Integer id){
		Actor actor = actorDao.getActorById(id);
		if(null!=actor){
			actor.setFuncs(actorRightDao.getActorRight(actor.getId()));
		}
		return actor;
	}
	/**
	 * 查询角色
	 * @param actor 角色
	 * @return
	 */
	public PageResultSet<Actor> queryActor(Actor actor,int page,int pageSize){
		return actorDao.queryActor(actor, page, pageSize);
	}
	
	/**
	 * 添加角色
	 * @param actor
	 * @return
	 */
	@Transactional 
	public boolean addActor(Actor actor){
		Integer newId = actorDao.addActor(actor);
		if(null!=newId){
			if(!actorRightDao.addActorRight(newId, actor.getFunCodes()))
				return false;
		}
		return (null==newId)?false:true;
	}
	
	/**
	 * 修改角色
	 * @param actor
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	public boolean updateActor(Actor actor) throws Exception{
		//只修改权限
		boolean flag = false;
		actorDao.updateActor(actor);
		if(actorRightDao.delActorRight(actor.getId())){
			flag =actorRightDao.addActorRight(actor.getId(), actor.getFunCodes());
			actorRightDao.delUserFunc(actor.getId(),actor.getFunCodes());
		}
		return flag;
	}
	
	public ArrayList<Actor> getAllActors() {
		ArrayList<Actor> actors = actorDao.getAllActors();
		if(null!=actors){
			for(int i=0;i<actors.size();i++){
				Actor temActor = actors.get(i);
				temActor.setFuncs(actorRightDao.getActorRight(temActor.getId()));
			}
		}
		return actors;
	}
	public String getDefaultActoName() {
		// TODO Auto-generated method stub
		return null;
	}
}
